var globalData 					= new Object();

	globalData.loginUrl 		= 'http://192.168.1.72:8081/ep/portal.do?userLogin';		//用户登陆接口接口地址
	globalData.getQuestionUrl 	= 'http://192.168.1.72:8081/ep/portal.do?question';	//获取题目接口
	globalData.saveAnswerUrl 	= 'http://192.168.1.72:8081/ep/portal.do?answer';		//保存用户答案
	globalData.saveAnswerLogUrl = 'http://192.168.1.72:8081/ep/portal.do?log';		//保存用户操作记录
	globalData.saveQuizUrl 		= 'test.php';		//交卷
	globalData.uploadUrl 		= 'upload.php';		//上传地址
	globalData.delFileUrl 		= 'upload.php';		//移除文件的事件


	// globalData.loginUrl 		= 'login.php';		//用户登陆接口接口地址
	// globalData.getQuestionUrl 	= 'getQuestion.php';	//获取题目接口
	// globalData.saveAnswerUrl 	= 'test.php';		//保存用户答案
	// globalData.saveAnswerLogUrl = 'test.php';		//保存用户操作记录
	// globalData.saveQuizUrl 		= 'test.php';		//交卷
	// globalData.uploadUrl 		= 'upload.php';		//上传地址
	// globalData.delFileUrl 		= 'upload.php';		//移除文件的事件
jQuery(document).ready(function() {
	
	//	接收url参数 并初始化页面
	var parm = myFun.getUrlParam('parm')
	myFun.initialize( parm );
	
	//	进入页面则获取题目信息
	// myFun.getQuestionInfo( parm , getCookie( 'currentQIdNumber' ) );

	/*点击上一题 或者 下一题*/
	$('.data-form').on('click','.btn-prev-next',function(){
		if( $(this).attr('disabled') == 'disabled' ){
			return false;
		}
		//	要获取的题目信息的题目编号
		questionId	 	 = $(this).attr('question-id');
		
		isGroup 		 = getCookie('isGroup');
		//	判断当前题目是否题组 然后获取当前的题目编号 序号 类型
		if( isGroup == 1 ){
			currentQIdNumber = parseInt(getCookie('currentChildrenQIdNumber'));
			currentQSort 	 = parseInt(getCookie('currentChildrenQSort'));
			questionType 	 = parseInt(getCookie('currentChildrenQType'));
		}else{
			currentQIdNumber = parseInt(getCookie('currentQIdNumber'));
			currentQSort 	 = parseInt(getCookie('currentQSort'));
			questionType 	 = parseInt(getCookie('currentQType'));
		}
		//	先保存答案，在获取题目信息 保存当前题目的答案
		answer = myFun.saveAnswer( parm, currentQIdNumber, questionType , isGroup );
		//	保存用户在页面上的操作记录
		myFun.saveAnswerLog( parm , $(this).attr('data-type') , currentQIdNumber , answer , questionType );
		//	判断上一题或下一题是否是题组
		if( $(this).attr('question-group') == 'true' ){
			$(".data-101").removeClass('show');
			$(".data-"+currentQIdNumber).removeClass('show');
			$(".data-"+currentQIdNumber).addClass('hide');

			$(".data-"+questionId).removeClass('hide');
			$(".data-"+questionId).addClass('show');

			setCookie('currentChildrenQIdNumber',questionId);
			setCookie('currentChildrenQType',$(this).attr('question-type'));

			if( $(this).attr('id') == 'btn-prev' ){
				setCookie('currentChildrenQSort',currentQSort-1);
			}

			if( $(this).attr('id') == 'btn-next' ){
				setCookie('currentChildrenQSort',currentQSort+1);
			}
			//	设置按钮状态及属性值
			myFun.setBtnAttribute();
		}else{
			//	获取题目信息并设置按钮状态及属性值
			myFun.getQuestionInfo(parm , questionId);
		}
	});
	
	/*点击答题卡*/
	$('.data-form').on('click','.quesiton-number li',function(){
		if( $(this).hasClass('active') ){
			return false;
		}
		//	下一个题目的编号
		QIdNumber	 	 = parseInt($(this).attr('question-id'));
		//	当前题目的信息
		currentQIdNumber = parseInt(getCookie('currentQIdNumber'));
		questionType 	 = parseInt(getCookie('currentQType'));

		//	保存当前题目的答案
		answer = myFun.saveAnswer( parm, currentQIdNumber, questionType , getCookie('isGroup') );

		//	保存用户在页面上的操作记录
		myFun.saveAnswerLog( parm , $(this).attr('data-type') , currentQIdNumber , answer , questionType );

		//	获取题目信息
		myFun.getQuestionInfo(parm , QIdNumber);
	});

	/*点击单选框*/
	$('.data-form').on('change','input[type=radio]',function(){
		$(".show .btn-label").removeClass("active");
		$(this).parent(".btn-label").addClass("active");
		//	记录用户在页面上的操作
		log( $(this).attr('data-type') );
	});

	/*点击多选框*/
	$('.data-form').on('change','input[type=checkbox]',function(){
		if( $(this).parent(".btn-label").hasClass("active") ){
			$(this).parent(".btn-label").removeClass("active");
		}else{
			$(this).parent(".btn-label").addClass("active");
		}
		//	记录用户在页面上的操作
		log( $(this).attr('data-type') );
	});

	/*点击交卷按钮*/
	$('.data-form').on('click','#btn-submit',function(){

		isGroup 		 = getCookie('isGroup');
		//	判断当前题目是否题组 然后获取当前的题目编号 序号 类型
		if( isGroup == 1 ){
			currentQIdNumber = parseInt(getCookie('currentChildrenQIdNumber'));
			questionType 	 = parseInt(getCookie('currentChildrenQType'));
		}else{
			currentQIdNumber = parseInt(getCookie('currentQIdNumber'));
			questionType 	 = parseInt(getCookie('currentQType'));
		}
		//	保存当前题目的答案
		answer = myFun.saveAnswer( parm, currentQIdNumber, questionType , isGroup );

		//	记录用户在页面上的操作
		log( $(this).attr('data-type') );
		//	交卷
		myFun.saveQuiz(parm);
	});

	/*单个移除文件*/
	$('.data-form').on('click','.kv-file-remove',function(){
		questionId = getCookie('currentQIdNumber');
		key = $(this).attr('data-key');
		answer = globalData[questionId]['answer'];
		globalData[questionId]['answer'] = [];
		i = 0;
		$.each( answer , function(k,v){
			if( k != key ){
				globalData[questionId]['answer'][i] = v;
				i = i + 1;
			}
		});
	});

	/*移除 新增的全部文件*/
	$('.data-form').on('click','.fileinput-remove',function(){
		questionId = getCookie('currentQIdNumber');
		answer = globalData[questionId]['answer'];
		globalData[questionId]['answer'] = [];
		i = 0;
		$.each( answer , function(k,v){
			if( v[2] == undefined || v[2] == null ){
				globalData[questionId]['answer'][i] = v;
				i = i + 1;
			}
		});
	});

	function log( eType ){
		//	先获取页面上的答案内容
		isGroup 		 = getCookie('isGroup');
		//	判断当前题目是否题组 然后获取当前的题目编号 序号 类型
		if( isGroup == 1 ){
			currentQIdNumber = parseInt(getCookie('currentChildrenQIdNumber'));
			questionType 	 = parseInt(getCookie('currentChildrenQType'));
		}else{
			currentQIdNumber = parseInt(getCookie('currentQIdNumber'));
			questionType 	 = parseInt(getCookie('currentQType'));
		}
		answer = myFun.getAnswer( currentQIdNumber, questionType );
		//	保存用户在页面上的操作记录
		myFun.saveAnswerLog( parm , eType , currentQIdNumber , answer , questionType );
	}
	
});
//	题号
var qNumberHtml 		= '<li class="{class}" id="id_{qID}" question-id="{qID}" question-sort="{qSort}" data-type="4">{qSort}</li>';
var qNumberFind 		= ['{class}','{qID}','{qID}','{qSort}','{qSort}'];
var qNumberReplace		= new Array();

//	题目标题
var qTitleHtml			= '<h3>题目 {qSort} / {qIdCount} </h3><span class="label label-info pull-left tag-{tagId}">{tagName}</span><p class="title">{title}</p> {img}';
var qTitleFind 			= ['{qSort}','{qIdCount}','{tagId}','{tagName}','{title}','{img}'];
var qTitleReplace 		= new Array();

//	答题内容
var qContentHtml		= new Array();
var qContentFind 		= new Array();
var qContentReplace 	= new Array();
	//	单选
	qContentHtml[0]		= '<p><label class="btn btn-default btn-label {class}" ><input type="radio" name="{inputName}" value="{value}" {checked} data-type="6" > {des} </label></p>';
	qContentFind[0] 	= ['{value}','{inputName}','{class}','{checked}','{des}'];
	qContentReplace[0] 	= [];
	
	//	多选
	qContentHtml[1]		= '<p><label class="btn btn-default btn-label {class}" ><input type="checkbox" name="{inputName}[]" value="{value}" {checked} data-type="6" > {des} </label></p>';
	qContentFind[1] 	= ['{value}','{inputName}','{class}','{checked}','{des}'];
	qContentReplace[1] 	= [];

	//	简单
	qContentHtml[2]		= '<textarea name="{inputName}" placeholder="..." class="form-control {class}" data-type="7" >{value}</textarea>';
	qContentFind[2] 	= ['{value}','{inputName}','{class}','{checked}','{des}'];
	qContentReplace[2] 	= [];

	//	上传
	qContentHtml[3]		= '{value}<input id="upload-file" type="file" multiple class="file" data-overwrite-initial="false" data-min-file-count="1">';
	qContentFind[3] 	= ['{value}'];
	qContentReplace[3] 	= [];

var myFun = {
	
	/*获取url参数方法*/
	getUrlParam : function (name) {
	    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	    var r = window.location.search.substr(1).match(reg);
	    if (r != null) return unescape(r[2]); return null;
	},

	/**接口0 .用户登陆接口*/
	initialize : function ( parm ) {

		parm = $.trim(parm);
		if( parm == '' || parm == null ){
			html = '<div id="cont_404">'
					+ '<img class="png" src="assets/img/404.png" />'
					+ '<img class="png msg" src="assets/img/404_msg.png" />'
					// + '</div><div id="cloud_404" class="png"></div>';
			$("body").html(html);
			return false;
		}
		$.ajax({
			type 	: "POST",
			url 	: globalData.loginUrl,
			dataType: "json",
			async	: false,
			data 	: { 'p' : parm },
			success	: function(data){
				if( data.status == 0 ){
					//	计算题目的个数
					data.qIdCount = count(data.qIdList);
					//	把返回数据注册到全局数组中保存
					globalData.init = data;
	    			myFun.setAnswerNumberList( data.qIdList );
	    		}else{
	    			html = '<div id="cont_404">'
							+ '<img class="png" src="assets/img/404.png" />'
							+ '<img class="png msg" src="assets/img/404_msg.png" />'
							// + '</div><div id="cloud_404" class="png"></div>';
					$("body").html(html);
	    		}
			},
			error : function (XMLHttpRequest, textStatus, errorThrown) {
				// html = '<div id="cont_404">'
				// 			+ '<img class="png" src="assets/img/404.png" />'
				// 			+ '<img class="png msg" src="assets/img/404_msg.png" />'
				// 			// + '</div><div id="cloud_404" class="png"></div>';
				// 	$("body").html(html);
			    // 通常 textStatus 和 errorThrown 之中
			    // 只有一个会包含信息
			    // 调用本次AJAX请求时传递的options参数
			    console.log(XMLHttpRequest);
			    console.log(textStatus);
			    console.log(errorThrown);
			}
		});
	},

	/*初始化设置设置答题卡列表*/
	setAnswerNumberList : function ( qIdList ){
		//	首次初始化答题卡列表
		var html = '';
		currentQIdNumber = getCookie('currentQIdNumber');
		$.each(qIdList, function(k, v) {
			cla = ''
			if( currentQIdNumber != '' && currentQIdNumber != null ){
				if( currentQIdNumber == v.id ){
					cla = 'active ';
					//	把当前应答题目的编号以及序号保存到cookie中
					setCookie( 'currentQIdNumber' , v.id );
					setCookie( 'currentQSort' , k );
				}
			}else{
				if( k == 1 ){
					cla = 'active ';
					setCookie( 'currentQIdNumber' , v.id );
					setCookie( 'currentQSort' , k );
				}
			}

			if( v.answer != '' && v.answer != null ){
				cla += ' over';
			}
			qNumberReplace = [cla,v.id,v.id,k,k];
			html += replaceAll( qNumberFind, qNumberReplace, qNumberHtml );
		});
		$(".quesiton-number").html(html);
	},

	/**接口1 .获取题目信息*/
	getQuestionInfo : function ( parm , qNumber ){
		if( qNumber == '' || qNumber == null ){
			qNumber = globalData.init.qIdList['1'].id;
		}
		// $("form.data-form").hide();
		$.ajax({
			type 	: "POST",
			url 	: globalData.getQuestionUrl,
			dataType: "json",
			async	: false,
			data 	: { 'p' : parm , 'qNumber' : qNumber },
			success	: function(data){
				if( data.status === 0 ){
					//	设置题目的内容区域 标题 和 表单
					myFun.setQuestionContent( data.qInfo );
				}else{

				}
			}
		});
	},

	/**设置题目页面内容*/
	setQuestionContent : function( qInfo ){
		var img = ' ';
		if( qInfo.img != undefined && qInfo.img != '' && qInfo.img != null ){
			img = '<p><img src="'+qInfo.img+'" title="" alt="" /></p>'
		}
		qTitleReplace 	= [qInfo.sort, globalData.init.qIdCount, qInfo.tagId, qInfo.tagName, qInfo.title, img];
		title 			= replaceAll( qTitleFind , qTitleReplace , qTitleHtml );
		content 		= '';
		if( qInfo._children != undefined && qInfo._children != null && qInfo._children != '' ){
			//	如果是题组 则把这组题的信息保存在全局中使用
			// qInfo['hasAnswerNumber'] = 0;	//	已答题目的个数
			globalData[qInfo.id] = qInfo;
			currentChildrenQIdNumber = getCookie( 'currentChildrenQIdNumber' );
			currentChildrenQSort 	 = getCookie( 'currentChildrenQSort' );
			$.each( qInfo._children,function(k,v){
				if( currentChildrenQIdNumber != null && currentChildrenQSort != null ){
					if( v.id == currentChildrenQIdNumber || v.sort == currentChildrenQSort ){
						cla = 'show';
						//	把当前应答题目的编号以及序号保存到cookie中
						setCookie( 'currentChildrenQIdNumber' , v.id );
						setCookie( 'currentChildrenQSort' , v.sort );
						setCookie( 'currentChildrenQType', v.type );
					}else{
						cla = 'hide';
					}
				}else{
					if( v.sort == 1 ){
						cla = 'show';
						//	把当前应答题目的编号以及序号保存到cookie中
						setCookie( 'currentChildrenQIdNumber' , v.id );
						setCookie( 'currentChildrenQSort' , v.sort );
						setCookie( 'currentChildrenQType', v.type );
					}else{
						cla = 'hide';
					}
				}
				
				title 			+= '<p class="data-'+v.id+' '+cla+'">';
				title 			+= v.sort+'.'+v.title;
				title 			+= '</p>';

				content 		+= '<div class="data-'+v.id+' '+cla+'">';
				if( ( v.type == 0 || v.type == 1 ) ){
					content 	+= myFun.getOptionHtml( v );
				}else{
					content 	+= myFun.getTextareaHtml( v );
				}
				content 		+= '</div>';
			});
			//	当前题目是否是题组 1 是 0 否
			setCookie('isGroup',1);
		}else{
			globalData[qInfo.id] = qInfo;
			if( ( qInfo.type == 0 || qInfo.type == 1 ) && qInfo.optionList != null ){
				content 		= myFun.getOptionHtml( qInfo );
			}else if( qInfo.type == 3 ){
				content 		= myFun.getUploadHtml( qInfo );
			}else{
				content 		= myFun.getTextareaHtml( qInfo );
			}
			//	当前题目是否是题组 1 是 0 否
			setCookie('isGroup',0);
		}
		//	设置当前题目编号 和题顺序号保存在cookie中
		setCookie('currentQIdNumber',qInfo.id);
		setCookie('currentQSort',qInfo.sort);
		setCookie('currentQType', qInfo.type );

		$("#question-title").html(title);
		$("#question-content").html(content);
		if( qInfo.type == 3 ){
			myFun.initUpload(qInfo);
		}
		// $("form.data-form").show();

		//	修正按钮属性
		myFun.setBtnAttribute( qInfo );
	},

	/**设置按钮的属性*/
	setBtnAttribute : function( qInfo ){
		prevQuestionId = nextQuestionId = prevQuestionType = nextQuestionType = 0;
		currentQIdNumber 		= parseInt(getCookie('currentQIdNumber'));
		currentQSort 	 		= parseInt(getCookie('currentQSort'));
		//	总的题数 	默认题组的题数量是1
		totalQuestion	 		= parseInt(globalData.init.qIdCount);
		currentChildrenQSort 	= totalChildrenQuestion = 1;
		if( qInfo == '' || qInfo == null ){
			qInfo = globalData[currentQIdNumber];
		}
		
		//	判断是否是题组形式	题组则是隐藏和显示
		if( qInfo._children != undefined && qInfo._children != null && qInfo._children != '' ){
			currentChildrenQIdNumber 	= parseInt(getCookie('currentChildrenQIdNumber'));
			currentChildrenQSort 	 	= parseInt(getCookie('currentChildrenQSort'));
			//	总的题数
			totalChildrenQuestion	 	= parseInt(count(qInfo._children));

			//	如果当前题目是题组的最后一题 则获取下一题的规则改变
			if( totalChildrenQuestion == currentChildrenQSort  ){
				$("#btn-prev").attr('question-group',true);
				$("#btn-next").attr('question-group',false);
				prevQuestionSort 	 	= currentChildrenQSort - 1;
				nextQuestionSort	 	= currentQSort + 1;
				$.each(qInfo._children, function(k, v) {
					if( prevQuestionSort == v.sort ){
						prevQuestionId 	= v.id;
						prevQuestionType= v.type;
					}
				});
				$.each(globalData.init.qIdList,function(k, v) {
					if( nextQuestionSort == k ){
						nextQuestionId 	= v.id;
					}
				});
			}else if( currentChildrenQSort == 1 ){
				$("#btn-prev").attr('question-group',false);
				$("#btn-next").attr('question-group',true);
				prevQuestionSort 	 	= currentQSort - 1;
				nextQuestionSort 	 	= currentChildrenQSort + 1;
				$.each(globalData.init.qIdList,function(k, v) {
					if( prevQuestionSort == k ){
						prevQuestionId 	= v.id;
					}
				});
				$.each(qInfo._children, function(k, v) {
					if( nextQuestionSort == v.sort ){
						nextQuestionId  = v.id;
						nextQuestionType= v.type;
					}
				});
			}else{
				$("#btn-prev").attr('question-group',true);
				$("#btn-next").attr('question-group',true);
				prevQuestionSort 	 	= currentChildrenQSort - 1;
				nextQuestionSort 	 	= currentChildrenQSort + 1;
				//	 设置上一题和下一题的题目编号
				$.each(qInfo._children, function(k, v) {
					if( prevQuestionSort == v.sort ){
						prevQuestionId  = v.id;
						prevQuestionType= v.type;
					}
					if( nextQuestionSort == v.sort ){
						nextQuestionId  = v.id;
						nextQuestionType= v.type;
					}
				});
			}
		}else{
			$("#btn-prev").attr('question-group',false);
			$("#btn-next").attr('question-group',false);
			prevQuestionSort 		 = currentQSort - 1;
			nextQuestionSort 		 = currentQSort + 1;
			$.each(globalData.init.qIdList,function(k, v) {
				if( prevQuestionSort == k ){
					prevQuestionId 		= v.id;
				}
				if( nextQuestionSort == k ){
					nextQuestionId 		= v.id;
				}
			});
		}
		$("#btn-prev").attr("question-id",prevQuestionId);
		$("#btn-next").attr("question-id",nextQuestionId);
		$("#btn-prev").attr("question-type",prevQuestionType);
		$("#btn-next").attr("question-type",nextQuestionType);
		//	如果是第一题则上一题按钮禁用
		if( currentQSort == 1 && currentChildrenQSort == 1 ){
			$("#btn-prev").attr("disabled",true);
		}else{
			$("#btn-prev").attr("disabled",false);
		}
		//	如果是最后一题则下一题按钮禁用
		if( currentQSort == totalQuestion && currentChildrenQSort == totalChildrenQuestion ){
			$("#btn-next").attr("disabled",true);
		}else{
			$("#btn-next").attr("disabled",false);
		}

		//	在答题卡列表中将当前题目选中
		$(".quesiton-number li").removeClass('active');
		$("#id_"+currentQIdNumber).addClass('active');
	},
	/**获取当前题目的答案*/
	getAnswer : function( questionId , questionType ){
		var answer;
		//	根据题目类型获取答案并判断
		if( questionType == 0){
			answer = $('input[name="answer['+questionId+']"]:checked').val();
		}else if( questionType == 1 ){
			answer = new Array();
			$('input[name="answer['+questionId+'][]"]:checked').each(function(){  
			    answer.push($(this).val());//向数组中添加元素  
			});
		}else if( questionType == 2 ){
			answer = $('textarea[name="answer['+questionId+']"]').val();
		}else{
			answer = []
			$.each( globalData[questionId]['answer'] , function(k,v){
				answer[k] = [];
				answer[k][0] = v[0];
				answer[k][1] = v[1];
			});
		}
		return answer;
	},
	/**保存答案 并设置*/
	saveAnswer : function( parm, questionId, questionType , isGroup ){
		answer = myFun.getAnswer( questionId, questionType );
		//	如果答案为空 则不提交答案
		if( answer == '' || answer == null || answer.length == 0  ){
			return answer;
		}

		//	提交保存数据
		$.ajax({
			type 	: "POST",
			url 	: globalData.saveAnswerUrl,
			dataType: "json",
			async	: false,
			data 	: { 'p' : parm , 'aId' : questionId , 'qType' : questionType , 'answer' : answer },
			success	: function(data){
				if( data.status != 0 ){

	    		}
			}
		});
		
		//	设置当前题目答题卡的状态是否已经答过
		if( isGroup == 0 ){
			$("#id_"+questionId).addClass('over');
		}else{
			//	题组时获取父编号
			qId = parseInt(getCookie('currentQIdNumber'));
			//	获取当前子题目的排序值
			qChildrenSort 	 = parseInt(getCookie('currentChildrenQSort'));
			n = 0;
			globalData[qId]['_children'][qChildrenSort-1]['answer'] = answer;
			$.each( globalData[qId]['_children'] , function(k,v){
				if( v.answer != '' && v.answer != null && v.answer.length > 0 ){
					n = n+1;
				}
			});
			//	如果答不为空的个数等于题目的数量
			if( n == count(globalData[qId]['_children']) ){
				$("#id_"+qId).addClass('over');
			}else{
				$("#id_"+qId).removeClass('over');
			}
		}

		return answer;
	},

	/**保存当前用户操作的痕迹*/
	saveAnswerLog : function( parm , eType , aId , answer , qType ){
		$.ajax({
			type 	: "POST",
			url 	: globalData.saveAnswerLogUrl,
			dataType: "json",
			async	: true,
			data 	: { 'p' : parm , 'eType' : eType , 'aId' : aId , 'answer' : answer , 'qType' : qType },
			success	: function(data){
				if( data.status != 0 ){

	    		}
			}
		});
		return true;
	},

	/**交卷*/
	saveQuiz : function(parm){
		$.ajax({
			type 	: "POST",
			url 	: globalData.saveQuizUrl,
			dataType: "json",
			async	: true,
			data 	: { 'p' : parm },
			success	: function(data){
				if( data.status != 0 ){

	    		}
			}
		});
		return true;
	},

	/**得到选项类型的html元素标签*/
	getOptionHtml : function( qInfo ){
		var html = '';
		$.each( qInfo.optionList, function(k,v){
			checked = cla = '';
			if( qInfo.answer != null && qInfo.answer != '' && qInfo.answer.indexOf(v.id) !== -1 ){
				cla = 'active';
				checked = 'checked';
			}
			qContentReplace[qInfo.type] = [v.id,'answer['+qInfo.id+']',cla,checked,v.des];
			html += replaceAll( qContentFind[qInfo.type] , qContentReplace[qInfo.type] , qContentHtml[qInfo.type] );
		});
		return html;
	},

	/**得到简答题目的html元素标签*/
	getTextareaHtml : function( qInfo ){
		qContentReplace[qInfo.type] = [qInfo.answer,'answer['+qInfo.id+']',];
		return replaceAll( qContentFind[qInfo.type] , qContentReplace[qInfo.type] , qContentHtml[qInfo.type] );
	},

	/**得到上传题目的html元素标签*/
	getUploadHtml : function( qInfo ){
		//	把数据保存到全局数组中
		globalData[qInfo.id] = qInfo;
 		qContentReplace[qInfo.type] = [];
		return replaceAll( qContentFind[qInfo.type] , qContentReplace[qInfo.type] , qContentHtml[qInfo.type] );
	},

	/*初始化上传插件*/
	initUpload : function( qInfo ){
		var initialPreview 	= new Array();
		var initialPreviewConfig = new Array();
		if( qInfo.answer != '' && qInfo.answer != null ){
			globalData.delFileUrl 
			
			$.each( qInfo.answer , function(k,v){
				var file = new Array(); //定义一数组 
				file = v[1].split("."); //字符分割 
				delFileUrl = globalData.delFileUrl+'?qNumber='+getCookie('currentQIdNumber')+'&filename='+file[0]+'&ext='+file[1];
				initialPreview.push("<div class='file-preview-text'>" + "<h2><i class='glyphicon glyphicon-file'></i></h2>" + v[0] + "</div>");
				obj = {
			        // caption: 'desert.jpg', 
			        width: '120px', 
			        url: delFileUrl, 		//	删除的链接地址
			        key: k,
			        extra: {}
			    }
				initialPreviewConfig.push(obj);
			});
		}
		qNumber = getCookie('currentQIdNumber');
		uploadUrl = globalData.uploadUrl+'?qNumber='+qNumber;
		// 初始化上传插件
		$("#upload-file").fileinput({
	        uploadUrl: uploadUrl, // you must set a valid URL here else you will get an error
	        allowedFileExtensions : [],
	        overwriteInitial: false,
	        maxFileSize: 100000,
	        maxFilesNum: 100,
	        showUpload: true, 	//是否显示上传按钮
		    showPreview: true,	//是否显示文件的预览图
		    showRemove: true,	//是否显示删除/清空按钮
		    showCancel: true,	//是否显示取消文件上传按钮。只有在AJAX上传线程中该属性才可见可用。默认值true。
		    browseClass: "btn btn-primary", //按钮样式  
		    previewClass: 'file-view',	//在预览区域容器上的额外的class。类型string。
		    mainClass: '',	//添加在文件上传主容器。类型string。
		    initialPreview: initialPreview,
		    initialPreviewConfig: initialPreviewConfig,
	        slugCallback: function(filename) {
	            return filename.replace('(', '_').replace(']', '_');
	        },
		});
		$("#upload-file").on("filepreupload",function(event, data, previewId, index){
			
		});
		//上传完后的回调操作
		$("#upload-file").on("fileuploaded", function(event, data, previewId, index) {
			questionId = getCookie('currentQIdNumber');
			var responseJSON = [];
			var form = data.form, files = data.files, extra = data.extra, response = data.response, reader = data.reader;
			responseJSON[0] = data.jqXHR.responseJSON[0];
			responseJSON[1] = data.jqXHR.responseJSON[1];
			responseJSON[2] = 1;	//	是新增文件 	全部移除的时候移除这些
			len = globalData[questionId]['answer'].length;
			//	把上传的数据保存到全局数组中
			globalData[questionId]['answer'][len] = responseJSON;
		});
	}
}

/*批量替换 find 和 replace 的长度必须一直且搜索值和替换值 一一对应
 * find 要查找的值 		数组格式
 * replace 要替换的值	数组格式
 * string 被搜索替换的字符串
 */
function replaceAll(find,replace,string){
	$.each(find,function(k,v){
		if( replace[k] == '' || replace[k] == null ){
			replace[k] = ' ';
		}
		string = string.replace(v,replace[k]);
	});
	return string;
}
/* js cookie操作 */
function setCookie(name,value)  //兩個參數，一個是cookie的名字，一個是值
{
    var Days = 5;               //此 cookie 將被保存7天
    var exp  = new Date();      //new Date("December 31, 9998");
    exp.setTime(exp.getTime() + Days*24*60*60*1000);
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
function getCookie(name)        //取 cookies函數       
{
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
    if(arr != null) return unescape(arr[2]); return null;

}
function delCookie(name)       //刪除 cookie
{
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=getCookie(name);
    if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}

 
/**
 * 取String 或者 object的长度
 * 
 */
function count(o)
{
    var t = typeof o;
    if(t == 'string'){
            return o.length;
    }else if(t == 'object'){
            var n = 0;
            for(var i in o){
                    n++;
            }
            return n;
    }
    return false;
}; 
